{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "visible-treaty",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "from argparse import Namespace\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "modern-third",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scripts.train import DataModule, _to_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "preceding-valuable",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../config/cups.json', 'rt') as f:\n",
    "    keypoint_config = json.load(f)\n",
    "module = DataModule(Namespace(train='/home/ken/data/cups_train/', val='/home/ken/data/cups_test/', batch_size=1, workers=1, pool=32), keypoint_config=keypoint_config)\n",
    "module.setup('fit')\n",
    "\n",
    "train_loader = module.train_dataloader()\n",
    "train_iterator = iter(train_loader)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "killing-bullet",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "def resize(target, width=640, height=360):\n",
    "    return cv2.resize(target, (width, height))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "soviet-ceramic",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "frame, target, depth, centers = next(train_iterator)\n",
    "\n",
    "plt.figure(figsize=(14, 8))\n",
    "image = _to_image(frame[0].numpy())\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        axis = plt.subplot2grid((2, 2), loc=(i, j))\n",
    "        axis.imshow(image)\n",
    "        axis.imshow(resize(target[0, i * 2 + j].numpy()), alpha=0.5)\n",
    "        plt.axis('off')\n",
    "plt.tight_layout()\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "descending-resource",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(14, 8))\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        axis = plt.subplot2grid((2, 2), loc=(i, j))\n",
    "        axis.imshow(image)\n",
    "        axis.imshow(resize(target[0, i * 2 + j].numpy()), alpha=0.5)\n",
    "        axis.imshow(resize((np.abs(centers[0].numpy()) > 1e-1).any(axis=0).astype(np.float32)), alpha=0.5, vmin=0.0, vmax=1.0)\n",
    "        plt.axis('off')\n",
    "plt.tight_layout()\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loved-swing",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectors = centers.numpy()[0]\n",
    "indices = np.zeros((2, 180, 320), dtype=np.uint16)\n",
    "for i in range(180):\n",
    "    for j in range(320):\n",
    "        indices[:, i, j] = [j, i]\n",
    "plt.figure(figsize=(12, 10))\n",
    "plt.quiver(vectors[0], vectors[1], units='xy', scale_units='xy', scale=1.0)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disciplinary-parker",
   "metadata": {},
   "outputs": [],
   "source": [
    "pixel_indices = np.zeros((2, 180, 320), dtype=np.float32)\n",
    "for i in range(180):\n",
    "    for j in range(320):\n",
    "        pixel_indices[:, i, j] = [j + 0.5, i + 0.5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "micro-abuse",
   "metadata": {},
   "outputs": [],
   "source": [
    "norms = np.linalg.norm(vectors, axis=0)\n",
    "where_non_zero = target[0].sum(axis=0) > 0.5\n",
    "p_centers = np.zeros((2, pixel_indices.shape[1], pixel_indices.shape[2]), dtype=np.float32)\n",
    "p_centers[:, where_non_zero] = pixel_indices[:, where_non_zero] + vectors[:, where_non_zero]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "narrative-stanford",
   "metadata": {},
   "outputs": [],
   "source": [
    "dotted_image = cv2.resize(image.copy(), (320, 180))\n",
    "for point in p_centers[:, where_non_zero].transpose():\n",
    "    cv2.circle(dotted_image, (point[0], point[1]), 2, (255, 0, 0), -1)\n",
    "plt.imshow(dotted_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "central-contemporary",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert p_centers[:, where_non_zero].shape[1] > 3\n",
    "center_points = np.unique(p_centers[:, where_non_zero].round().astype(np.int32), axis=1)\n",
    "assert center_points.shape[1] >= 1 and center_points.shape[1] < 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01c4b265-c08f-41c6-9970-fe0a4c340379",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_centers[:, where_non_zero].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffa0aa22-b817-4bf3-a59b-e7eb57ce6ad4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vision",
   "language": "python",
   "name": "vision"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
